﻿using Assessment.BLL;
using Assessment.Model;
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public partial class Admin_Gauge_GaugeTypeEdit : BasePage
{
    #region 属性

    /// <summary>
    /// Id
    /// </summary>
    private int Id
    {
        get
        {
            return ViewState["Id"] == null ? 0 : Convert.ToInt32(ViewState["Id"]);
        }
        set
        {
            ViewState["Id"] = value;
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return ViewState["Action"] == null ? "Add" : ViewState["Action"].ToString();
        }
        set
        {
            ViewState["Action"] = value;
        }
    }

    #endregion

    T_GaugeType model = new T_GaugeType();
    T_GaugeTypeBLL bll = new T_GaugeTypeBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        //base.CheckUserPermission("001001");
        if (!IsPostBack)
        {
            Id = base.ParseQueryInt("Id", 0);
            Action = base.ParseQueryString("Action", "Add");

            if (Action == "Modify" || Action == "Detail")
            {
                getDate(Id);
            }
            if (Action == "Detail")
            {
                this.txt_Title.Enabled = false;
                this.txt_Remark.Enabled = false;
                this.ch_Valid.Enabled = false;

                this.btn_Modify.Visible = true;
                this.btn_Save.Visible = false;
            }
            else if (Action == "Add" || Action == "Modify")
            {
                this.txt_Title.Enabled = true;
                this.txt_Remark.Enabled = true;
                this.ch_Valid.Enabled = true;

                this.btn_Modify.Visible = false;
                this.btn_Save.Visible = true;
            }
        }
    }
    #endregion

    #region 绑定数据
    /// <summary>
    /// 加载数据
    /// </summary>
    /// <param name="id"></param>
    private void getDate(int id)
    {
        model = bll.GetModel(id);
        this.txt_Title.Text = model.Title;
        this.txt_Remark.Text = model.Remark;
        this.ch_Valid.Checked = (model.Valid == 1);
    }

    #endregion

    #region 提交
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        //检验数据是否重复
        string strCheck = string.Format("(Title like '{0}') and GaugeTypeId<>{1}",
            TextUtility.ReplaceText(this.txt_Title.Text.Trim()),
            Id.ToString());

        if (bll.GetRecordCount(strCheck) > 0)
        {
            base.Alert("类型名称已存在，请重新输入！");
            return;
        }

        if (Action == "Modify")
        {
            model = bll.GetModel(Id);
        }
        else if (Action == "Add")
        {
            model = new T_GaugeType();
        }
        else
        {
            base.Alert("异常操作！");
            return;
        }
        model.Title = this.txt_Title.Text.Trim();
        model.Remark = this.txt_Remark.Text.Trim();
        
        model.Valid = this.ch_Valid.Checked ? 1 : 0;


        try
        {
            if (Action == "Modify")
            {
                //修改
                bll.Update(model);
            }
            else if (Action == "Add")
            {
                //保存
                Id = bll.Add(model);
            }
            base.Redirect("操作成功！", "GaugeTypeEdit.aspx?Action=Detail&Id=" + Id.ToString());
        }
        catch (Exception ex)
        {
            base.Alert("操作失败！");
        }
    }
    #endregion

    #region 编辑按钮
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        Response.Redirect("GaugeTypeEdit.aspx?Action=Modify&Id=" + Id);
    }
    #endregion
}